package com.amazon.spring8amazon.controller;

import com.amazon.spring8amazon.common.Result;
import com.amazon.spring8amazon.common.StrTools;
import com.amazon.spring8amazon.common.TestReadFile;
import com.amazon.spring8amazon.domain.Params;
import com.amazon.spring8amazon.domain.YibaiAmazonPublishSkuSelectResult;
import com.amazon.spring8amazon.domain.YibaiSystemAccount;

import com.amazon.spring8amazon.mapper.YibaiAmazonPublishSkuSelectResultMapper;
import com.amazon.spring8amazon.mapper.YibaiSystemAccountMapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/amazonCenter")
//@MapperScan("com.amazon.spring8amazon.mapper")
@DS("master")
public class AmazonCenterController {

    @Autowired
    private YibaiAmazonPublishSkuSelectResultMapper yibaiAmazonPublishSkuSelectResultMapper;

    @GetMapping("/id")
    public List<YibaiAmazonPublishSkuSelectResult> getDataById() {
        System.out.println(yibaiAmazonPublishSkuSelectResultMapper.selectMany());
        return yibaiAmazonPublishSkuSelectResultMapper.selectMany();
    }


    @Autowired
    private YibaiSystemAccountMapper yibaiSystemAccountMapper;
    @GetMapping("/accountId")
    public YibaiSystemAccount getAccountById(Params params) {
        return yibaiSystemAccountMapper.selectAccountAndSelect(params);
    }


    // 根据publishId查询选品信息
    @GetMapping("/publishId")
    public Result getDescByPublishId(Params params) {
        List<YibaiAmazonPublishSkuSelectResult> info = yibaiAmazonPublishSkuSelectResultMapper.selectResultByPublishId(params);
        return Result.success(info);
    }


  //测试分页查询
    @GetMapping("/Page")
    public List<YibaiAmazonPublishSkuSelectResult> getProductListPage(Params params){
        return yibaiAmazonPublishSkuSelectResultMapper.selectManyByPage(params);
    }



    // 测试读取原生mysql数据
    @GetMapping("/sql")
    public Result getSqlData(Params params) throws IOException {
        try {
//            String sqlPath = "src/main/resources/sql/amazonCenter/test.sql";
            String sqlPath = "classes/sql/amazonCenter/test.sql";
            String sql = TestReadFile.readFile(sqlPath);

            String sku = params.getSku();
            StringBuilder skuBuilder = StrTools.StrMerge(sku);
            // System.out.println(skuBuilder); // 打印参数
            // 利用纯生sql语句执行查询
            List<Map<String, Object>> mapList = SqlRunner.db().selectList(String.format(sql, skuBuilder));
            return Result.success(mapList);
        }
        catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }



    // 根据中台刊登ID得到变体属性值
    @GetMapping("/VariationPublishId")
    public Result getAmazonVariationByPublishId(Params params) throws IOException {
        try {
//            String sqlPath = "src/main/resources/sql/amazonCenter/AmazonVariationByPublishId.sql";
//            String sqlPath = "classes/sql/amazonCenter/AmazonVariationByPublishId.sql";
//            String sql = TestReadFile.readFile(sqlPath);
//            StringBuilder argBuilder = StrTools.StrMerge(params.getPublishId()); // 传递参数刊登Id 支持模糊查询
//            // System.out.println(argBuilder); // 打印参数
//            // 利用纯生sql语句执行查询
//            List<Map<String, Object>> mapList = SqlRunner.db().selectList(String.format(sql, argBuilder));
            //System.out.println(mapList);
            List<HashMap<String,Object>> mapList = yibaiAmazonPublishSkuSelectResultMapper.selectPublishVariByPid(params);


            return Result.success(mapList);
        }
        catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }



    // 根据中台刊登时间区间刊登报错码对应统计条数数据
    @GetMapping("/PublishNumByTime")
    public Result getAmazonPublishNumByTime(Params params) throws IOException {
        try {
//            String sqlPath = "src/main/resources/sql/amazonCenter/AmazonPublishNumByTime.sql";
//            String sqlPath = "classes/sql/amazonCenter/AmazonPublishNumByTime.sql";
//            String sql = TestReadFile.readFile(sqlPath);
//            String startTime = params.getStartTime();
//            String endTime = params.getEndTime();

//            System.out.println(startTime + "~" + endTime); // 打印参数
            // 利用纯生sql语句执行查询
//            List<Map<String, Object>> mapList = SqlRunner.db().selectList(String.format(sql, startTime ,endTime))
//
            List<HashMap<String, Object>> mapList = yibaiAmazonPublishSkuSelectResultMapper.selectPublishFailByTime(params);
            return Result.success(mapList);
        }
        catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }













}
